import { Module } from 'vuex'
import { IRootState } from '@/store/type'
import { Ibanner } from './type'
import { bannerRequest } from '@/service/home/home'
import cache from '@/utils/cache/cache'
const bannerModule: Module<Ibanner, IRootState> = {
  namespaced: true,
  state() {
    return {
      bannerImgList: [],
      referralBannerImgList: []
    }
  },
  getters: {
    getDataList(state) {
      return (pageName: string) => {
        switch (pageName) {
          case '/banner?type=2':
            return state.bannerImgList
            break
          case '/personalized?limit=10':
            return state.referralBannerImgList
            break
        }
      }
    }
  },
  mutations: {
    changeBannerImgList(state, payLoad: any) {
      state.bannerImgList = payLoad
    },
    changeReferralBannerImgList(state, payLoad: any) {
      state.referralBannerImgList = payLoad
    }
  },
  actions: {
    async getBannerList({ commit }, pageName: string) {
      const bannerImgs = await bannerRequest(pageName)
      switch (pageName) {
        case '/banner?type=2':
          commit('changeBannerImgList', bannerImgs.banners)
          cache.setCache('bannerImgList', bannerImgs.banners)
          break
        case '/personalized?limit=10':
          commit('changeReferralBannerImgList', bannerImgs.result)
          cache.setCache('referralBannerImgList', bannerImgs.result)
          break
      }
    },
    setStore({ commit }) {
      commit('changeBannerImgList', cache.getCache('bannerImgList'))
      commit('changeReferralBannerImgList', cache.getCache('referralBannerImgList'))
    }
  }
}
export default bannerModule
